package com.fxc.handler;

import com.fxc.api.MemberService;
import com.fxc.crowd.constant.CrowdConstant;
import com.fxc.crowd.util.ResultEntity;
import com.fxc.entity.PO.MemberPO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author FXC
 * @version 1.0
 * @date 2021/12/3 0:18
 */
@RestController
public class MemberProviderHandler {

    @Autowired
    private MemberService memberService;

    @RequestMapping("/save/member/remote")
    public ResultEntity<String> saveMember(@RequestBody MemberPO memberPO){

        try {
            memberService.saveMember(memberPO);

            return ResultEntity.successWithData();
        } catch (Exception e) {

            if (e instanceof DuplicateKeyException){
                return ResultEntity.failed(CrowdConstant.MESSAGE_LOGIN_ACCT_ALREADY_IN_USE);
            }

            return ResultEntity.failed(e.getMessage());
        }
    }

    @RequestMapping("/get/member/login/acct/remote")
    ResultEntity<MemberPO> getMemberPOByLoginAcctRemote(@RequestParam("loginacct") String loginacct){

        try {

            // 调用Service进行查询
            MemberPO memberPO = memberService.getMemberService(loginacct);

            // 2如果没有抛异常，那就返回成功的结果
            return ResultEntity.successWithData(memberPO);
        } catch (Exception e) {

            e.printStackTrace();
            // 3.如果捕获到异常则返回失败的结果
            return ResultEntity.failed(e.getMessage());
        }

    }
}
